﻿using Cinch;
using FR.SAFTAnalyser.Model;
using FR.SAFTAnalyser.Services;
using MEFedMVVM.Common;
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;

namespace FR.SAFTAnalyser.ViewModels
{
    public class HeaderViewModel : WorkspaceViewModel
    {
        public HeaderViewModel()
        {
            if (!Designer.IsInDesignMode)
            {
                Mediator.Instance.Register(this);
                this.DisplayName = "Cabeçalho";
            }
        }

        [MediatorMessageSink(MessageType.SAFT_FILE_OPENED)]
        private void LoadHeader(AuditFile saft)
        {
            this.Cabecalho = saft.Header;
        }

        Header cabecalho;
        public Header Cabecalho
        {
            get
            {
                if (cabecalho == null && SAFT.SaftAnalyser.Instance.SaftFile != null)
                    cabecalho = SAFT.SaftAnalyser.Instance.SaftFile.Header;
                return cabecalho;
            }
            set
            {
                cabecalho = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<HeaderViewModel>(x => x.Cabecalho));
            }
        }

        public string Pais
        {
            get
            {
                if (this.Cabecalho != null && this.Cabecalho.CompanyAddress != null)
                {
                    if (this.Cabecalho.CompanyAddress.Country is System.Xml.XmlNode[])
                    {
                        System.Xml.XmlNode[] nodes = this.Cabecalho.CompanyAddress.Country as System.Xml.XmlNode[];
                        if (nodes != null && nodes.Length > 0)
                            return (this.Cabecalho.CompanyAddress.Country as System.Xml.XmlNode[])[0].InnerText;
                    }
                    else if (this.Cabecalho.CompanyAddress.Country != null)
                        return this.Cabecalho.CompanyAddress.Country.ToString();
                }
                return "";
            }
            set { NotifyPropertyChanged(ObservableHelper.CreateArgs<HeaderViewModel>(x => x.Pais)); }
        }
    }
}
